<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Role information class.
 *
 * Role information's CRUD.
 */
class Role_model extends CI_Model
{
	private $db_name = 'k9deploy_new';
	private $table = 'roles';
	private $db = null;
	
	/**
	 * Constructor
	 *
	 * Sets the database used for information.
	 */
	public function __construct()
	{
		parent::__construct();
		
		$this->db = $this->load->database($this->db_name, TRUE);
		
		$this->load->library('memcached_lib');
	}
	
	/**
	 * 获取出ROOT以外的所有用户组
	 *
	 * @access  public
	 * @return  object
	 */
	public function get_roles()
	{
// 		return $this->db->where('id <>', '1')->get($this->table)->result();
		return $this->db->get($this->table)->result();
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * 根据用户组ID获取用户信息
	 *
	 * @access  public
	 * @param   int
	 * @return  object
	 */
	public function get_role_by_id($id)
	{
		return $this->db->where('id', $id)->get($this->table)->row();
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * 根据用户组名称获取用户组信息
	 *
	 * @access  public
	 * @param   string
	 * @return  object
	 */
	public function get_role_by_name($name)
	{
		return $this->db->where('name', $name)->get($this->table)->row();
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * 格式化数组成ASSOC方式
	 *
	 * @access  private
	 * @param   array
	 * @param   string
	 * @param   string
	 * @return  array
	 */
	private function _re_parse_array($array, $key, $value)
	{
		$data = array();
		foreach ($array as $v)
		{
			$data[$v->$key] = $v->$value;
		}
		return $data;
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * 添加用户组
	 *
	 * @access  public
	 * @param   array
	 * @return  int
	 */
	public function add_role($data)
	{
		$this->db->insert($this->table, $data);
		return $this->db->insert_id();
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * 修改用户组
	 *
	 * @access  public
	 * @param   int
	 * @param   array
	 * @return  bool
	 */
	public function edit_role($id, $data)
	{
		return $this->db->where('id', $id)->update($this->table, $data);
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * 删除用户组
	 *
	 * @access  public
	 * @param   int
	 * @return  void
	 */
	public function del_role($id)
	{
		$this->db->where('id', $id)->delete($this->table);
	}
}
